<?php
/**
* @todo falta dar soporte para prepared statments y reever la metadata que devuelve el objeto
*/
require_once "interface/Gi_Driver_Interface.php";
class Gi_Driver_Mysqli implements Gi_Driver_Interface {
  private $_db;  // base de datos actual
  /**
  * @var mysqli
  */
  private $_mysqli; //objeto mysqli
  /**
  * @var mysqli_result
  */
  private $_result; //objeto mysqli_result
  /**
  * @var mysqli_stmt
  */
  private $_stmt; //objeto mysqli_stmt
  
  public function __construct ($driver, $host, $db = null, $port=null, $user=null, $pass=null, $driver_options = null) {
    $this->_mysqli = new mysqli($host,$user,$pass,$db,$port);
    if (mysqli_connect_errno($this->_mysqli)) { 
      echo 'Conexion fallida: ' . mysqli_connect_error($this->_mysqli);
    }
    if ($db !== null) {
      $this->_db = $db;        
    }
  }
  
  public function get_db_metadata ($db = null) {
    
    if ($db === null) {
      $db = $this->_db;
    }
    $sql = "
      SELECT t.TABLE_NAME,t.TABLE_TYPE
      FROM information_schema.tables t
      WHERE t.TABLE_SCHEMA = '{$db}';
    ";
    $this->execute($sql);
    
    while ($row = $this->fetch_assoc()) {
      $data[] = $row;
    }  
    return $data;
  }
  
  public function get_table_metadata ($table,$db=null) {
    if ($db === null) {
      $db = $this->_db;
    }
    $sql = "
      SELECT t.TABLE_NAME,t.TABLE_TYPE ,c.COLUMN_NAME, c.DATA_TYPE
      FROM information_schema.tables t
      LEFT JOIN information_schema.columns c on t.TABLE_SCHEMA = c.TABLE_SCHEMA and t.TABLE_NAME = c.TABLE_NAME
      WHERE t.TABLE_SCHEMA = '{$db}' AND c.TABLE_NAME = '{$table}';
    ";
    $this->execute($sql);
    
    while ($row = $this->fetch_assoc()) {
      $data[] = $row;
    }  
    return $data;
  }
  
  public function set_db ($db) {
    $this->_db = $db;
    $this->_mysqli->select_db($db);
    return $this;
  }
  
  public function error_code () {
    return $this->_mysqli->errno;
  }
  
  public function error_info () {
    return $this->_mysqli->error;
  }
  
  public function execute ($sql) {
    $this->_result = $this->_mysqli->query($sql);
    
    if (!$this->_result){
      throw new Exception("no se pudo ejecutar el query, Mysql informo error: " . $this->error_info());
      return false;
    }
    return true;
  }
  
  public function last_insert_id ($name = null) {
    $sql = "SELECT LAST_INSERT_ID();";
    $qid = $this->execute($sql);
    $result = $qid->fetch_row();
    $qid->close();
    return (int) $result[0];
  }
  
  public function get_db_handler () {
    return $this->_mysqli;
  }
  
  public function begin_transaction () {
    return $this->_mysqli->autocommit(false);
  }
  
  public function commit () {
    $this->_mysqli->commit(); 
    return $this->_mysqli->autocommit(true);
  }
  
  public function roll_back() {
    $this->_mysqli->rollback();
    return $this->_mysqli->autocommit(true);
  }
  
  public function close () {
    return $this->_mysqli->close();
  }
  
  public function fetch_assoc () {
    if (!($this->_result instanceof mysqli_result)) {
      throw new Exception("no se ha ejecutado nigun query");
    }
    return $this->_result->fetch_assoc();
  }
  
  public function stmt_fetch_assoc() {
  }
  
  public function prepare ($sql , $opt = null) {
  }
  
  public function stmt_execute($sql, array $params = null) {
  }
}
?>
